/*
 * MediaSourceT.h
 *
 *  Created on: 2015年12月31日
 *      Author: terry
 */

#ifndef SRC_MEDIASOURCET_H_
#define SRC_MEDIASOURCET_H_

#include "MediaSource.h"
#include "TCriticalSection.h"


namespace av
{

template < class T >
class MediaSourceT : public T
{
public:
	MediaSourceT():
		m_state(STATE_STOPPED)
	{
	}

	virtual ~MediaSourceT()
	{
	}


    virtual bool getFormat(MediaFormat& fmt)
    {
        if (!m_format.isValid())
        {
            return false;
        }

        fmt = m_format;
        return true;
    }

    virtual int getDuration()
    {
        return m_format.m_duration;
    }


    virtual bool seekable()
    {
        return false;
    }

    virtual int seek(int64_t offset)
    {
        return 0;
    }

    virtual int setScale(float scale)
    {
        return 0;
    }

    virtual float getScale()
    {
        return 1.0f;
    }

    virtual bool isLive()
    {
        return true;
    }

    virtual bool getMetaData(const std::string& key, std::string& value)
    {
        return false;
    }

	virtual int play()
	{
		setState(STATE_PLAYING);
		return 0;
	}

	virtual int pause()
	{
		setState(STATE_PAUSED);
		return 0;
	}

	virtual void stop()
	{
		setState(STATE_STOPPED);
	}

	void setState(StreamState state)
	{
		comn::AutoCritSec lock(m_cs);
		m_state = state;
	}

	virtual int getState()
	{
		return m_state;
	}


protected:

protected:
	comn::CriticalSection m_cs;
	MediaFormat	m_format;
	StreamState	m_state;

};


} /* namespace av */

#endif /* SRC_MEDIASOURCET_H_ */
